﻿using QldtSdh.DAL.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace QldtSdh.BLL
{
    class HeThong
    {
        internal static DAL.Models.AppUser GetNguoiSuDung(string tenDangNhap, string matKhau)
        {
            if (tenDangNhap == null || matKhau == null)
                return null;
            tenDangNhap = tenDangNhap.ToLower();
            using (var context = CEMS4UG_DHKHContext.GetContext())
            {
                var au = context.AppUserDbSet.Where(e => e.UserName.ToLower() == tenDangNhap && e.Password == matKhau).FirstOrDefault();
                return au;
            }

        }

        public static List<AppUser> GetList(bool isOnlyActive = false)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                if (isOnlyActive)
                    return db.AppUserDbSet.Where(e => e.IsActive == true).OrderBy(e => e.FullName).ToList();
                else
                    return db.AppUserDbSet.OrderBy(e => e.UserName).ToList();
            }
        }

        internal static DAL.Models.AppUser GetNguoiSuDung(string tenDangNhap)
        {
            if (tenDangNhap == null)
                return null;
            tenDangNhap = tenDangNhap.ToLower();
            using (var context = CEMS4UG_DHKHContext.GetContext())
            {
                var au = context.AppUserDbSet.Where(e => e.UserName.ToLower() == tenDangNhap).FirstOrDefault();
                return au;
            }
        }



        internal static void DoiMatKhau(AppUser m_NguoiSuDung, string matKhauMoi)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                AppUser nguoiSuDung = db.AppUserDbSet.Where(
                    nsd => nsd.UserName == m_NguoiSuDung.UserName &&
                        nsd.Password == m_NguoiSuDung.Password).FirstOrDefault();
                if (nguoiSuDung == null)
                    throw new Exception("Thông tin người sử dụng không đúng!");
                else
                {
                    try
                    {
                        NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                        BizNhatKyHoatDong.InitData<AppUser>(nhatKy, nguoiSuDung, nguoiSuDung.UserName, LOAIDOITUONG.NGƯỜI_SỬ_DỤNG, LOAITHAOTAC.Chỉnh_sửa);

                        nguoiSuDung.Password = matKhauMoi;
                        db.SaveChanges();
                        BizNhatKyHoatDong.Add<AppUser>(nhatKy, nguoiSuDung);
                    }
                    catch
                    {
                        throw new Exception("Không thể thay đổi mật khẩu được!");
                    }
                }
            }
        }

        internal static CODE_RESULT_RETURN Update(AppUser nguoiSuDung)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var ds = (from e in db.AppUserDbSet
                          where e.UserName == nguoiSuDung.UserName
                          select e).SingleOrDefault();
                if (ds != null)
                {
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData<AppUser>(nhatKy, ds, ds.UserName, LOAIDOITUONG.NGƯỜI_SỬ_DỤNG, LOAITHAOTAC.Chỉnh_sửa);

                    ds.FullName = nguoiSuDung.FullName;
                    ds.Password = nguoiSuDung.Password;
                    ds.Role = nguoiSuDung.Role;
                    ds.IsActive = nguoiSuDung.IsActive;
                    db.SaveChanges();
                    BizNhatKyHoatDong.Add<AppUser>(nhatKy, ds);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                return CODE_RESULT_RETURN.ThatBai;
            }
        }

        internal static CODE_RESULT_RETURN Add(AppUser nguoiSuDung)
        {
            using (var context = CEMS4UG_DHKHContext.GetContext())
            {
                var ds = (from e in context.AppUserDbSet
                          where e.UserName == nguoiSuDung.UserName
                          select e).SingleOrDefault();
                if (ds != null)
                    return CODE_RESULT_RETURN.MaTrung;
                else
                {
                    context.AppUserDbSet.Add(nguoiSuDung);
                    context.SaveChanges();
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData<AppUser>(nhatKy, null, nguoiSuDung.UserName, LOAIDOITUONG.NGƯỜI_SỬ_DỤNG, LOAITHAOTAC.Thêm);
                    BizNhatKyHoatDong.Add<AppUser>(nhatKy, nguoiSuDung);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
            }
        }

        internal static CODE_RESULT_RETURN Delete(AppUser SelectedNguoiSuDung)
        {
            using (var context = CEMS4UG_DHKHContext.GetContext())
            {
                var nsd = (from e in context.AppUserDbSet
                           where e.UserName == SelectedNguoiSuDung.UserName
                           select e).SingleOrDefault();
                if (nsd != null)
                {
                    try
                    {
                        if ((nsd.ListBangDiemLopHocPhan_DaDuyet != null && nsd.ListBangDiemLopHocPhan_DaDuyet.Count > 0) ||
                            (nsd.ListBangDiemLopHocPhan_DaKiemTra != null && nsd.ListBangDiemLopHocPhan_DaKiemTra.Count > 0) ||
                            (nsd.ListLopHocPhan_DaDuyetBangDiemQTHT != null && nsd.ListLopHocPhan_DaDuyetBangDiemQTHT.Count > 0) ||
                            (nsd.ListLopHocPhan_DaNhapBangDiemQTHT != null && nsd.ListLopHocPhan_DaNhapBangDiemQTHT.Count > 0))
                            return CODE_RESULT_RETURN.ThatBai;
                        else
                        {
                            context.AppUserDbSet.Remove(nsd);
                            context.SaveChanges();
                            NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                            BizNhatKyHoatDong.InitData<AppUser>(nhatKy, nsd, nsd.UserName, LOAIDOITUONG.NGƯỜI_SỬ_DỤNG, LOAITHAOTAC.Xóa);
                            BizNhatKyHoatDong.Add<AppUser>(nhatKy, null);
                            return CODE_RESULT_RETURN.ThanhCong;
                        }
                    }
                    catch
                    {
                        return CODE_RESULT_RETURN.ThatBai;
                    }
                }
                return CODE_RESULT_RETURN.ThatBai;
            }
        }
    }
}
